<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="户型" prop="huxing">
      <el-input v-model="dataForm.huxing" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="suqare">
      <el-input v-model="dataForm.suqare" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="所属小区" prop="xiaoqu">
      <el-input v-model="dataForm.xiaoqu" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="楼栋号" prop="dongId">
      <el-input v-model="dataForm.dongId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="房间编号" prop="houseNum">
      <el-input v-model="dataForm.houseNum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="所属工程" prop="projectId">
      <el-input v-model="dataForm.projectId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="户主id" prop="hostId">
      <el-input v-model="dataForm.hostId" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          huxing: '',
          suqare: '',
          xiaoqu: '',
          dongId: '',
          houseNum: '',
          projectId: '',
          hostId: ''
        },
        dataRule: {
          huxing: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          suqare: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          xiaoqu: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dongId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          houseNum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          projectId: [
            { required: false, message: '不能为空', trigger: 'blur' }
          ],
          hostId: [
            { required: false, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/house/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.huxing = data.house.huxing
                this.dataForm.suqare = data.house.suqare
                this.dataForm.xiaoqu = data.house.xiaoqu
                this.dataForm.dongId = data.house.dongId
                this.dataForm.houseNum = data.house.houseNum
                this.dataForm.projectId = data.house.projectId
                this.dataForm.hostId = data.house.hostId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/house/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'huxing': this.dataForm.huxing,
                'suqare': this.dataForm.suqare,
                'xiaoqu': this.dataForm.xiaoqu,
                'dongId': this.dataForm.dongId,
                'houseNum': this.dataForm.houseNum,
                'projectId': this.dataForm.projectId,
                'hostId': this.dataForm.hostId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
